﻿using System.ComponentModel;

namespace Mundialeitor2.Helpers
{
    public static class MyEnumExtensions
    {
        public static string ToNameString(this EquipoEnum val)
        {
            var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return ToDescriptionString(attributes, 0);
        }

        public static string ToCodeString(this EquipoEnum val)
        {
            var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return ToDescriptionString(attributes, 1);
        }

        public static string ToSedeString(this SedeEnum val)
        {
            var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : string.Empty;
        }

        private static string ToDescriptionString(DescriptionAttribute[] attributes, int index)
        {
            var result = string.Empty;

            if (attributes.Length > 0)
            {
                result = attributes[0].Description.Split('-')[index];
            }

            return result;
        }
    }
}